package com.kz.springboot.common.exception;

import com.kz.springboot.common.response.JsonResponse;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by wyz on 2017/3/21.
 * 统一异常处理
 */

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(value = BizServiceException.class)
    public JsonResponse bizErrorHandler(HttpServletRequest req, BizServiceException e){
        JsonResponse r = new JsonResponse();
        r.setErrorCode(e.getCode());
        r.setErrorMsg(e.getMessage());
        r.setState(500);
        return r;
    }

    @ExceptionHandler(value = BizServiceRuntimeException.class)
    public JsonResponse bizRuntimeErrorHandler(HttpServletRequest req, BizServiceRuntimeException e){
        JsonResponse r = new JsonResponse();
        r.setErrorCode(e.getCode());
        r.setErrorMsg(e.getMessage());
        r.setState(500);
        return r;
    }

    @ExceptionHandler(value = Exception.class)
    public JsonResponse defaultErrorHandler(HttpServletRequest req, Exception e) throws Exception {
        JsonResponse r = new JsonResponse();
        r.setErrorCode("500");
        r.setErrorMsg("系统错误");
        r.setState(-1);
        e.printStackTrace();
        return r;
    }

}